#coding:utf8
import os
import sys
import glob
import xlrd
from pyexcel_xls import get_data
from pyexcel_xls import save_data
from collections import OrderedDict
"""
这里使用到了一个第三方库。首先要安装pyexcel-xls
这个库可以将表格中的数据读取出来作为一个二维数组，
data = get_data(excel_name)； data.keys() 代表有n个sheet，
data[d] 代表一个sheet中的数据，他以列表嵌套的形式存储数据内容[[col1,col2,col3,col4,...]]


"""
    

def load_excel_1(excel_name):
	data_lst = []
	data = get_data(excel_name)
	for d in data.keys():
		item = data[d]
		for i in range(1,len(item)):
			data_lst.append(item[i])
		
	print len(data_lst)
	return data_lst


excels = glob.glob(sys.argv[1]+'/*.xlsx')

def main():
	data = OrderedDict()
	sheet_1 = []
	row_1_data = [u"excel_name",u"RowFileLine",u"LabelerId",u"标注账号",u"T",u"验收轮数",u"number_id",u"Result:有效性",u"Result:备注",u"QA:Result",u"QA:Comment",u"QA:QAID",u"质检账号",u"Inspect:Result","Inspect:Comment","Inspect:InspectorId","ID","flag_idx","time_flag"]
	#添加表头
    sheet_1.append(row_1_data)
	for xlsx_file in excels[:]:
		print xlsx_file
		data_xls = load_excel_1(xlsx_file)
		for dd in data_xls:
			xlsx_file_name = xlsx_file.decode('gbk')
			dd.insert(0,xlsx_file_name)
			sheet_1.append(dd)


	# 添加sheet表
	data.update({u"这是XX表":sheet_1})
 
    # 保存成xls文件
	save_data("result_test.xls", data)
		

if __name__ == '__main__':
	main()